#!/alidata/server/php/bin/php
<?php
error_reporting(0);
ini_set('memory_limit', '1000M');
set_time_limit(0);

$currList = array();
$lastList = array();
$arrBorough = array();
$last_key = '';
$grouplist = array();
while (($line_json = fgets(STDIN)) !== false) {
    $line_json = trim($line_json);
    if (empty($line_json))
        continue;
    if (!strstr($line_json, "\t")){
        echo $line_json."\n";
        continue;
    }
    list ($key, $house_info) = explode("\t", $line_json);
    list ($key, $source) = explode(".", $key);
    $house_info = json_decode($house_info, True);
    if (($last_key == $key) || ($last_key == '')){
         $grouplist[] = $house_info;
    }
    else {
        if ($grouplist != array()){
            countWeight($grouplist);
            $grouplist = array();
            $grouplist[] = $house_info;
        }
    }
    $last_key = $key;
}
countWeight($grouplist);
function countWeight($grouplist){
    $Tmpgrouplist = $grouplist;
    $flag_com = array();
	foreach ($grouplist as $key => $value){
		if($flag_com[$key]==1) continue;
		$max_weight=0;
        $max_key=0;
        $last_source=$grouplist[$key]['source'];
        $grouplist[$key]['weight'] = 0;
        $out_str = json_encode($grouplist[$key]);
        $flag_com[$key]=1;
		foreach($Tmpgrouplist as $tmpKey => $tmpValue){
            $diff_num=0;
            if (($last_source != $grouplist[$max_key]['source']) && ($max_weight > 35) ){
                $last_source = $grouplist[$max_key]['source'];
                $grouplist[$max_key]['weight'] = $max_weight;
                $out_str .= "\t".json_encode($grouplist[$max_key]);
                $flag_com[$max_key] = 1;
                $max_weight = 0;
                $max_key = 0; 
            }
			if($flag_com[$tmpKey]==1) continue;  //already grouped
			if($key==$tmpKey)  continue;  //same one
		    $weight=1;
            if ($value['house_hall'] == $tmpValue['house_hall']) {
                    $weight += '31.00';
					}
            else if (!empty($value['house_hall']) && !empty($tmpValue['house_hall'])){
                $diff_num=$diff_num + 1;
            }
            if ($tmpValue['house_price'] * 0.975 <= $value['house_price'] && $value['house_price'] <= $tmpValue['house_price'] * 1.025)      {
                    $weight_price = (1 - abs($tmpValue['house_price'] - $value['house_price']) / $tmpValue['house_price'] / 0.05) * 25.00;
                    $weight_price = $weight_price == 0 ? 1 : $weight_price;
                    $weight_price = $weight_price > 25 ? 25 : $weight_price;
                    $weight += $weight_price;
					}
			else{
				$weight = -1000;		
			}
            if ($value['house_fitment'] == $tmpValue['house_fitment']) {
                    $weight += '25.00';
					}
            else if(!empty($value['house_fitment']) && !empty($tmpValue['house_fitment'])){
                $diff_num=$diff_num + 1;
            }
            if (tVt($value['house_toward'], $tmpValue['house_toward'])) {
                    $weight += '12.00';
					}
            else if(!empty($value['house_toward']) && !empty($tmpValue['house_toward'])){
                $diff_num=$diff_num + 1;
            }
            
            if ($value['house_toilet'] == $tmpValue['house_toilet']) {
                    $weight += '7.00';
            }
            /*
            if (!empty($value['house_built_year']) && !empty($tmpValue['house_built_year'])) {
                if ($value['house_built_year'] != $tmpValue['house_built_year']){
                    $diff_num=$diff_num + 1;
                }
            }*/
            if ($diff_num > 1){
                $weight = 0;
            }
			if($weight>$max_weight)  {
                $max_weight=$weight; 
                $max_key = $tmpKey;
            }
		}
    echo 'max_weight='.$max_weight;
    echo $last_source.$grouplist[$max_key]['source'];
    if(($last_source != $grouplist[$max_key]['source']) && ($max_weight>35)) {
        echo 'in if';
	 	$grouplist[$max_key]['weight']=$max_weight; 	
        $flag_com[$max_key]=1;
        $out_str .= "\t".json_encode($grouplist[$max_key]);
	}
    echo $out_str."\n";
}
}

function tVt($t1,$t2){
    if($t1=='1'){
        if($t2 == '1' || $t2 == '5' || $t2 == '7' || $t2 == '10' || $t2 == '11' || $t2 == '12' || $t2 == '14' || $t2 == '15'){
            return true;
        }else{
            return false;
        }
    }
    if($t1=='2'){
        if($t2 == '2' || $t2 == '6' || $t2 == '8' || $t2 == '10' || $t2 == '11' || $t2 == '13' || $t2 == '14' || $t2 == '15'){
            return true;
        }else{
            return false;
        }
    }
    if($t1=='3'){
        if($t2 == '3' || $t2 == '5' || $t2 == '6' || $t2 == '9' || $t2 == '11' || $t2 == '12' || $t2 == '13' || $t2 == '15'){
            return true;
        }else{
            return false;
        }
    }
    if($t1=='4'){
        if($t2 == '4' || $t2 == '7' || $t2 == '8' || $t2 == '9' || $t2 == '12' || $t2 == '13' || $t2 == '14'  || $t2 == '15'){
            return true;
        }else{
            return false;
        }
    }
    if($t1=='5'){
        if($t2 == '5' || $t2 == '1' || $t2 == '3' || $t2 == '6' || $t2 == '7' || $t2 == '9' || $t2 == '10'  || $t2 == '11' || $t2 == '12' || $t2 == '13' || $t2 == '14'  || $t2 == '15'){
            return true;
        }else{
            return false;
        }
    }
    if($t1=='6'){
        if($t2 == '6' || $t2 == '2' || $t2 == '3' || $t2 == '5' || $t2 == '8' || $t2 == '9' || $t2 == '10' || $t2 == '11' || $t2 == '12' || $t2 == '13' || $t2 == '14'  || $t2 == '15'){
            return true;
        }else{
            return false;
        }
    }
    if($t1=='7'){
        if($t2 == '7' || $t2 == '1' || $t2 == '4' || $t2 == '5' || $t2 == '8' || $t2 == '9' || $t2 == '10' || $t2 == '11' || $t2 == '12' || $t2 == '13' || $t2 == '14'  || $t2 == '15'){
            return true;
        }else{
            return false;
        }
    }
    if($t1=='8'){
        if($t2 == '8' || $t2 == '2' || $t2 == '4' || $t2 == '6' || $t2 == '7' || $t2 == '9' || $t2 == '10' || $t2 == '11' || $t2 == '12' || $t2 == '13' || $t2 == '14'  || $t2 == '15'){
            return true;
        }else{
            return false;
        }
    }
    if($t1=='9'){
        if($t2 == '9' || $t2 == '3' || $t2 == '4' || $t2 == '5' || $t2 == '6' || $t2 == '7' || $t2 == '8' || $t2 == '11' || $t2 == '12' || $t2 == '13' || $t2 == '14'  || $t2 == '15'){
            return true;
        }else{
            return false;
        }
    }
    if($t1=='10'){
        if($t2 == '10' || $t2 == '1' || $t2 == '2' || $t2 == '5' || $t2 == '6' || $t2 == '7' || $t2 == '8' || $t2 == '11' || $t2 == '12' || $t2 == '13' || $t2 == '14'  || $t2 == '15'){
            return true;
        }else{
            return false;
        }
    }
    if($t1=='11'){
        if($t2 !== '4'){
            return true;
        }else{
            return false;
        }
    }
    if($t1=='12'){
        if($t2 !== '2'){
            return true;
        }else{
            return false;
        }
    }
    if($t1=='13'){
        if($t2 !== '1'){
            return true;
        }else{
            return false;
        }
    }
    if($t1=='14'){
        if($t2 !== '3'){
            return true;
        }else{
            return false;
        }
    }
    if($t1=='15'){
        return true;
    }
}
?>
